//
//  CellOfDetailsPagePostSearchResult.m
//  FreeTravel
//
//  Created by 徐康 on 16/9/2.
//  Copyright © 2016年 KX. All rights reserved.
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "CellOfDetailsPagePostSearchResult.h"
@interface CellOfDetailsPagePostSearchResult ()
@property (nonatomic, strong) UILabel *labelOfTitle;
@property (nonatomic, strong) UILabel *labelOfUserName;
@property (nonatomic, strong) UILabel *labelOfViews;
@property (nonatomic, strong) UILabel *labelOfReplys ;
@property (nonatomic, strong) UILabel *labelOfLastpost ;
@property (nonatomic, strong) UIImageView *imageViewOfAvatar;
@property (nonatomic, strong) UIView *grayView;
@end
@implementation CellOfDetailsPagePostSearchResult
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.grayView = [[UIView alloc]init];
        self.imageViewOfAvatar = [[UIImageView alloc]init];
        self.labelOfTitle = [[UILabel alloc]init];
        self.labelOfViews = [[UILabel alloc]init];
        self.labelOfReplys = [[UILabel alloc]init];
        self.labelOfLastpost = [[UILabel alloc]init];
        self.labelOfUserName = [[UILabel alloc]init];
        [self.contentView addSubview:self.grayView];
        [self.contentView addSubview:self.labelOfUserName];
        [self.contentView addSubview:_labelOfLastpost];
        [self.contentView addSubview:self.labelOfReplys];
        [self.contentView addSubview:self.labelOfViews];
        [self.contentView addSubview:self.labelOfTitle];
        [self.contentView addSubview:self.imageViewOfAvatar];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self.imageViewOfAvatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_offset(50);
        make.left.equalTo(self.contentView.mas_left).mas_offset(10);
        make.top.equalTo(self.contentView.mas_top).mas_offset(10);
    }];
    self.imageViewOfAvatar.layer.cornerRadius = 25;
    self.imageViewOfAvatar.layer.masksToBounds = YES;
    [self.labelOfUserName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).mas_offset(10);
        make.left.equalTo(self.imageViewOfAvatar.mas_right).mas_offset(10);
        make.right.equalTo(self.contentView);
        make.height.mas_offset(10);
    }];
    self.labelOfUserName.textColor = [UIColor grayColor];
    self.labelOfUserName.font = [UIFont systemFontOfSize:12];
    [self.grayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(5);
        
    }];
    self.grayView.backgroundColor = [UIColor grayColor];
    
    [self.labelOfViews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageViewOfAvatar.mas_right).mas_offset(10);
        make.width.mas_offset(150);
        make.height.mas_offset(10);
        make.bottom.equalTo(self.grayView.mas_top).mas_offset(-20);
    }];
    self.labelOfViews.textColor = [UIColor grayColor];
    self.labelOfViews.font = [UIFont systemFontOfSize:12];
    
    [self.labelOfReplys mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).mas_offset(-10);
        make.bottom.equalTo(self.grayView.mas_top).mas_offset(-20);
        make.height.mas_offset(10);
        make.width.mas_offset(50);
    }];
    self.labelOfReplys.textColor = [UIColor grayColor];
    self.labelOfReplys.font = [UIFont systemFontOfSize:12];
    [self.labelOfLastpost mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.labelOfReplys.mas_left).mas_offset(10);
        make.bottom.equalTo(self.grayView.mas_top).mas_offset(-20);
        make.height.mas_offset(10);
        make.width.mas_offset(100);
    }];
    [self.labelOfTitle mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(self.imageViewOfAvatar.mas_right).mas_offset(10);
        make.right.equalTo(self.contentView.mas_right).mas_offset(-10);
        make.top.equalTo(self.labelOfUserName.mas_bottom).mas_offset(10);
        
    }];
    self.labelOfTitle.numberOfLines = 0;
    
}
- (void)setModel:(ModelOfPostSpecificSearchResult *)model{
    _model = model;
    [self.imageViewOfAvatar sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
    self.labelOfTitle.text = model.title;
    NSString *str = [NSString stringWithFormat:@"%@人浏览",model.views];
    self.labelOfViews.text = str;
    NSString *str2 = [NSString stringWithFormat:@"%@人评论",model.replys];
    self.labelOfReplys.text = str2;
    self.labelOfUserName.text = model.username;
    // 时间戳转换
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyyMMddHHMMss"];
    NSDate *date = [formatter dateFromString:[NSString stringWithFormat:@"%ld",model.lastpost]];
    NSString *timeStr = [formatter stringFromDate:date];
    
    self.labelOfLastpost.text = timeStr;
}
@end
